package com.tjn.list_;

/**
 * @author 陶镓妮
 * @version 1.0
 * @description  关于LInkedList中的双链表
 */
public class LinkedList01 {
    @SuppressWarnings("all")
    public static void main(String[] args) {
        //模拟一个简单的双链表
        Node syz = new Node("孙燕姿");
        Node xzq = new Node("薛之谦");
        Node wsl = new Node("汪苏泷");

        //连接上面的三个结点，形成双链表
        syz.next =xzq;
        xzq.next = wsl;
        wsl.pre = xzq;
        xzq.pre = syz;

        //头结点first，尾结点last
        Node first = syz;
        Node last = wsl;

        //从头到尾遍历
        System.out.println("===从头到尾遍历====");
        while(true){
            if(first == null){
                break;
            }
            System.out.println(first);
            first = first.next;
        }

        //从尾到头遍历
        System.out.println("===从尾到头遍历===");
        while(true){
            if(last == null){
                break;
            }
            System.out.println(last);
            last = last.pre;
        }

        //插入节点"陶镓妮”到孙燕姿和薛之谦之间
        Node tjn = new Node("陶镓妮");
        tjn.next = xzq;
        tjn.pre = syz;
        syz.next = tjn;
        xzq.pre = tjn;

        //再次遍历需要再次获取头结点first
        first = syz;
        System.out.println("==插入数据后从头到尾遍历==");
        System.out.println("===从头到尾遍历====");
        while(true){
            if(first == null){
                break;
            }
            System.out.println(first);
            first = first.next;
        }
    }
}
class Node{
    public Object item;
    public Node next;
    public Node pre;

    public Node(Object item) {
        this.item = item;
    }

    @Override
    public String toString() {
        return "Node{" +
                "item=" + item +
                '}';
    }
}